package com.rz.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.admin.entity.GroupResource;
import com.rz.admin.entity.Res;
import com.rz.admin.mapper.GroupResourceMapper;
import com.rz.admin.service.IGroupResService;
import com.rz.admin.service.IResService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GroupResService extends ServiceImpl<GroupResourceMapper, GroupResource> implements IGroupResService {

    @Resource
    private IResService resService;

    @Override
    public List<GroupResource> getByGroupId(List<Integer> groupIds) {
        QueryWrapper<GroupResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("group_id",groupIds);
        return list(queryWrapper);
    }

    @Override
    public List<Integer> getResIdByGroupId(List<Integer> groupIds) {
        Set<Integer> resIds = new HashSet<>();
        List<GroupResource> resources = getByGroupId(groupIds);
        if(resources!=null && resources.size()>0){
            resources.forEach(roleResource -> resIds.add(roleResource.getResId()));
        }
        return new ArrayList<>(resIds);
    }

    @Override
    public Collection<Res> getResByGroupId(List<Integer> groupIds) {
        List<Integer> grs = getResIdByGroupId(groupIds);
        return grs!=null && grs.size()>0 ? resService.listByIds(grs) : new ArrayList<>();
    }
}
